<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
	GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
			new PurchaseModel());
	PurchaseModelExt queryCount = new PurchaseModelExt();
	int count = dal.count(queryCount);


	int pageIndex = MiscUtil.getIntUrlParam(request, "pageIndex", 1);

	int pageSize = MiscUtil.getIntUrlParam(request, "pageSize", 10);
	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	PurchaseModelExt query = new PurchaseModelExt();
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	
	query.setCountItem(true);
	
//	List<PurchaseModel> list = dal.selectList(query);
	List<PurchaseModel> list = CommonBll.loadPurchaseList(query);
	boolean embedded = MiscUtil.getBoolUrlParam(request, "embedded", false);
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Purchase List</title>
	<%if(!embedded){ %>
		<link rel="stylesheet" href="css/listForm.css" />
		<link rel="stylesheet" href="css/common.css" />
		<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
		<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
		<link rel="stylesheet" href="css/jquery-dialog.css" />
		
		<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
		<script language="javascript" type="text/javascript" src="js/common.js"></script>
	<%} %> 
	
	<script language="javascript" type="text/javascript">
		$(document).ready(pageLoaded);
		function pageLoaded()
		{	
			initDeleteConfirmation();
		} 
	</script>
</head>
<body>
	<%
		if (!embedded) {
	%>
	<div class="divContainer">
	<%@ include file="menu.jsp"%>
	<br />
	<a style="font-weight: bold;" href="purchaseNew.jsp">New purchase</a>
	<br />
	<br />
	<%}%>
	
	<div class="divListHeaderBar">NUMBER OF PURCHASES: <%=count%></div>
	<div class="divOverFlow">
	<table class="tblList">
		<tr class="tblListHeader">
			<th class="thListHeader functionColumnNormal">Purchase</th> 
			<th class="thListHeader Id" ></th>
			<th class="thListHeader Id" >Id</th>
			<th class="thListHeader" >Name</th>
			<th class="thListHeader" >Code</th>
			<th class="thListHeader" >Description</th>
			<th class="thListHeader" >Remark</th>
			<th class="thListHeader" >Status</th>
			<th class="thListHeader" >Purchaser</th>
			<th class="thListHeader" >Supplier</th>
			<th class="thListHeader" >Currency</th>
			<th class="thListHeader" >Additional Cost</th>
			<th class="thListHeader" >Discount</th>
			<th class="thListHeader" >Purchase Cost</th>
			<th class="thListHeader" >Payment Date</th>
			<th class="thListHeader" >Order Time</th>
			<th class="thListHeader" >Plan Delivery Time</th>
			<th class="thListHeader" >Real Delivery Time</th>
			<th class="thListHeader" >Project</th>
			<th class="thListHeader" >Event</th>
			<th class="thListHeader" >Checklist</th>
			<th class="thListHeader" >Task</th>
			<th class="thListHeader" >Quotation</th>
			<th class="thListHeader" >Quotation Item</th>
		</tr>

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {
				PurchaseModel obj = list.get(i);
		%>

		<tr class="trListItem">
			<td class="tdListInt">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="purchase?mode=view&id=<%=obj.getId()%>">View</a></span>
				<br/>
				<span class="spanEdit"><a href="purchase?mode=edit&id=<%=obj.getId()%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="purchase?mode=delete&id=<%=obj.getId()%>"  onclick="confirmToDelete();">Delete</a></span>
			</td>
			<td class="tdListHref">
					<a
						href="purchaseItemNew.jsp?idPurchase=<%=obj.getId()%>">Add item</a>
					<br/>
					[<%=obj.getItemCount()%>]
				</td>
			<td class="tdList"><%=obj.getIdToString()%></td>
			<td class="tdListText projectName"><%=obj.getNameToString()%></td>
			<td class="tdListText"><%=obj.getCodeToString()%></td>
			<td class="tdListTextarea"><%=obj.getDescriptionToString()%></td>
			<td class="tdListTextarea"><%=obj.getRemarkToString()%></td>
			<td class="tdListInt"><%=StringUtil.getPurchaseStatus(obj.getStatus())%></td>
			<td class="tdListText"><%=CacheUtil.getEmployeeName(obj.getPurchaser())%></td>
			<td class="tdListText"><%=CacheUtil.getCompanyName(obj.getSupplier())%></td>
			<td class="tdListText"><%=CacheUtil.getCurrencyName(obj.getIdCurrency())%></td>
			<td class="tdListText"><%=obj.getAdditionalCostToString()%></td>
			<td class="tdListText"><%=obj.getDiscountToString()%></td>
			<td class="tdListText"><%=obj.getPurchaseCostToString()%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateString(obj.getPaymentDate())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateTimeString(obj.getOrderTime())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateTimeString(obj.getPlanDeliveryTime())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateTimeString(obj.getRealDeliveryTime())%></td>
			<td class="tdListText"><%=CacheUtil.getProjectName(obj.getIdProject())%></td>
			<td class="tdListText"><%=CacheUtil.getEventName(obj.getIdEvent())%></td>
			<td class="tdListText"><%=CacheUtil.getChecklistName(obj.getIdChecklist())%></td>
			<td class="tdListText"><%=CacheUtil.getChecklistItemName(obj.getIdTask())%></td>
			<td class="tdListText"><%=CacheUtil.getQuotationName(obj.getIdQuotation())%></td>
			<td class="tdListText"><%=CacheUtil.getQuotationItemName(obj.getIdQuotationItem())%></td>
			
		</tr>
		<%
			}
		%>
	</table>
	</div>
	<br />
	<%
		if(noOfPage>1)
		{
	%>
	<table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging">
				<%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) {
				%> <span class="lblPaging"><%=i%></span> <%
 	} else {
 %> <a class="hrefPaging"
				href="purchaseList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
				<%
					}
					}
				%>
			</td>
		</tr>
	</table>
	<%} %>
	
	<%if(!embedded){ %> </div> <%} %>
</body>
</html>